package com.xingfly.repository;

import com.xingfly.model.Like;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface LikeRepository extends JpaRepository<Like, Long> {
    //根据userID objectType objectID 查询Like对象
    Like findByUserIdAndObjectTypeAndObjectId(Long userId, int objectType, Long objectId);

    //查询喜欢某个对象的用户数量
    @Query("SELECT COUNT(l) FROM Like l WHERE l.objectType=:type AND l.objectId=:id")
    Long findLikeUserCount(@Param("type") int objectType, @Param("id") Long objectId);

    //查询喜欢某个对象的用户列表
    @Query("SELECT l.user.id FROM Like l WHERE l.objectType=:type AND l.objectId=:id")
    List<Long> findLikeUserIds(@Param("type") int objectType, @Param("id") Long objectId);

    //查询用户喜欢对象的总量
    @Query("SELECT count(l) FROM Like l WHERE l.user.id=:id")
    Long findUserLikeCount(@Param("id") Long userId);
}
